#include "firstwindow.h"

#include <QPushButton>
#include <QIcon>
#include <QLabel>
#include <QPixmap>
#include <QTextEdit>
#include <QLineEdit>
#include <QFont>
#include <QLabel>
#include <QFile>
#include <QTextStream>
#include <QCheckBox>

void closeOrOpen(QPushButton& x1, QPushButton& x2, QPushButton& x3, QPushButton& x4, QPushButton& x5) {
    if(x1.isVisible()) {
        x1.setVisible(false);
        x2.setVisible(false);
        x3.setVisible(false);
        x4.setVisible(false);
        x5.setVisible(false);
    } else {
        x1.setVisible(true);
        x2.setVisible(true);
        x3.setVisible(true);
        x4.setVisible(true);
        x5.setVisible(true);
    }
}

FirstWindow::FirstWindow(QWidget *parent)
    : QMainWindow{parent}
{
    this->setWindowTitle("学生管理系统");
    this->setWindowIcon(QIcon(":/Picture/Picture/windowTitle.png"));
    this->setFixedSize(600,800);
    QLabel *backgroundLabel = new QLabel;
    backgroundLabel->setFixedSize(600,800);
    QPixmap backgroundPixmap(":/Picture/Picture//background.png");
    backgroundLabel->setPixmap(backgroundPixmap.scaled(backgroundLabel->size(), Qt::KeepAspectRatio));
    this->setCentralWidget(backgroundLabel);
    this->operateClass = new Class;
    QFile openfile("AllClassName.txt");
    openfile.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream openin(&openfile);
    QString line = openin.readLine(); // 读取第一行
    openfile.close(); // 关闭文件
    this->operateClass->className = line;
    //创建对话框以传达提示信息
    QTextEdit* textEdit = new QTextEdit(this);
    textEdit->setReadOnly(true); // 设置文本框为只读
    textEdit->setGeometry(70, 50, 460, 200); // 设置位置和大小
    textEdit->setPlainText("请选择所需功能！"); // 设置初始文本
    textEdit->setStyleSheet("QTextEdit { background: transparent;"
                            "border: 2px solid #000000; " //设置边框为2像素宽，黑色
                            "border-radius: 5px; " // 设置边框圆角半径
                            "transition: background-color 0.3s ease; " //添加过渡效果 }"); //设置文本框透明
                            "}");
    QFont font;
    font.setPointSize(18); // 设置字体大小
    font.setFamily("华文新魏"); // 设置字体族
    textEdit->setFont(font); //应用字体到文本框
    //添加功能：添加新的班级、修改所操作的班级，添加新的学生、修改某学生初始信息、删除某学生
    QPushButton * addNewClass = new QPushButton("添加新的班级",this);
    addNewClass->setFixedSize(240,70);
    addNewClass->move(180,290);
    addNewClass->setFont(font);
    addNewClass->setStyleSheet("QPushButton { "
                               "background-color: transparent; " //设置背景色为透明
                               "border: 2px solid #000000; " //设置边框为2像素宽，黑色
                               "border-radius: 5px; " // 设置边框圆角半径
                               "transition: background-color 0.3s ease; " //添加过渡效果
                               "}"
                               "QPushButton:hover { "
                               "background-color: #ADD8E6; " //鼠标悬停时设置背景色为浅蓝色
                               "}");
    QPushButton * modifyClass = new QPushButton("修改所操作的班级",this);
    modifyClass->setFixedSize(240,70);
    modifyClass->move(180,390);
    modifyClass->setFont(font);
    modifyClass->setStyleSheet("QPushButton { "
                               "background-color: transparent; " //设置背景色为透明
                               "border: 2px solid #000000; " //设置边框为2像素宽，黑色
                               "border-radius: 5px; " // 设置边框圆角半径
                               "transition: background-color 0.3s ease; " //添加过渡效果
                               "}"
                               "QPushButton:hover { "
                               "background-color: #ADD8E6; " //鼠标悬停时设置背景色为浅蓝色
                               "}");
    QPushButton * addNewStudent = new QPushButton("添加新的学生",this);
    addNewStudent->setFixedSize(240,70);
    addNewStudent->move(180,490);
    addNewStudent->setFont(font);
    addNewStudent->setStyleSheet("QPushButton { "
                                 "background-color: transparent; " //设置背景色为透明
                                 "border: 2px solid #000000; " //设置边框为2像素宽，黑色
                                 "border-radius: 5px; " // 设置边框圆角半径
                                 "transition: background-color 0.3s ease; " //添加过渡效果
                                 "}"
                                 "QPushButton:hover { "
                                 "background-color: #ADD8E6; " //鼠标悬停时设置背景色为浅蓝色
                                 "}");
    QPushButton * modifyStudent = new QPushButton("修改某学生初始信息",this);
    modifyStudent->setFixedSize(240,70);
    modifyStudent->move(180,590);
    modifyStudent->setFont(font);
    modifyStudent->setStyleSheet("QPushButton { "
                                 "background-color: transparent; " //设置背景色为透明
                                 "border: 2px solid #000000; " //设置边框为2像素宽，黑色
                                 "border-radius: 5px; " // 设置边框圆角半径
                                 "transition: background-color 0.3s ease; " //添加过渡效果
                                 "}"
                                 "QPushButton:hover { "
                                 "background-color: #ADD8E6; " //鼠标悬停时设置背景色为浅蓝色
                                 "}");
    QPushButton * deleteStudent = new QPushButton("删除某学生",this);
    deleteStudent->setFixedSize(240,70);
    deleteStudent->move(180,690);
    deleteStudent->setFont(font);
    deleteStudent->setStyleSheet("QPushButton { "
                                 "background-color: transparent; " //设置背景色为透明
                                 "border: 2px solid #000000; " //设置边框为2像素宽，黑色
                                 "border-radius: 5px; " // 设置边框圆角半径
                                 "transition: background-color 0.3s ease; " //添加过渡效果
                                 "}"
                                 "QPushButton:hover { "
                                 "background-color: #ADD8E6; " //鼠标悬停时设置背景色为浅蓝色
                                 "}");
    connect(addNewClass,&QPushButton::clicked,this,[=](){
        closeOrOpen(*addNewClass, *modifyClass, *addNewStudent, *modifyStudent, *deleteStudent);
        QLineEdit* lineEdit = new QLineEdit("请输入班级名字", this);
        lineEdit->setFont(font);
        lineEdit->setGeometry(100, 400, 400, 70);
        lineEdit->setStyleSheet(R"(QLineEdit {
                                     background-color: transparent; /* 设置背景为透明 */
                                     border: 2px solid #000000;
                                     border-radius: 5px;
                                     transition: background-color 0.3s ease;
                                     }
                                     QLineEdit:hover {
                                     background-color: #ADD8E6; /* 鼠标悬停时设置背景为浅蓝色 */
                                     })");
        lineEdit->show();
        QPushButton* okButton = new QPushButton("OK", this);
        okButton->setGeometry(120, 550, 120, 70);
        okButton->setFont(font);
        okButton->setStyleSheet("QPushButton { "
                                "background-color: transparent; " //设置背景色为透明
                                "border: 2px solid #000000; " //设置边框为2像素宽，黑色
                                "border-radius: 5px; " // 设置边框圆角半径
                                "transition: background-color 0.3s ease; " //添加过渡效果
                                "}"
                                "QPushButton:hover { "
                                "background-color: #ADD8E6; " //鼠标悬停时设置背景色为浅蓝色
                                "}");
        okButton->show();
        QPushButton * returnBack = new QPushButton("返回",this);
        returnBack->setGeometry(360, 550, 120, 70);
        returnBack->setFont(font);
        returnBack->setStyleSheet("QPushButton { "
                                  "background-color: transparent; " //设置背景色为透明
                                  "border: 2px solid #000000; " //设置边框为2像素宽，黑色
                                  "border-radius: 5px; " // 设置边框圆角半径
                                  "transition: background-color 0.3s ease; " //添加过渡效果
                                  "}"
                                  "QPushButton:hover { "
                                  "background-color: #ADD8E6; " //鼠标悬停时设置背景色为浅蓝色
                                  "}");
        returnBack->show();
        textEdit->setText("请输入新班级名，格式为：________班，完成后点击“OK”");
        connect(okButton, &QPushButton::clicked, [=](){
            QFile temp("AllClassName.txt");
            temp.open(QIODevice::ReadOnly | QIODevice::Text);
            QTextStream in(&temp);
            bool ifhave = false;
            while (!in.atEnd()) {
                QString line = in.readLine();
                if (line.contains(lineEdit->text())) {
                    textEdit->setText("该班级已存在！");
                    temp.close();
                    delete lineEdit;
                    delete okButton;
                    closeOrOpen(*addNewClass, *modifyClass, *addNewStudent, *modifyStudent, *deleteStudent);
                    ifhave = true;
                    break;
                }
            }
            if(ifhave==false) {
                temp.close();
                QFile file("AllClassName.txt");
                file.open(QIODevice::Append | QIODevice::Text);
                QTextStream out(&file);
                out << "\n" << lineEdit->text();
                file.close();
                QString temp1("Class/");
                QString temp2("班.txt");
                QString way;
                way = temp1 + lineEdit->text() + temp2;
                QFile temp3(way);
                temp3.open(QIODevice::WriteOnly | QIODevice::Text);
                temp3.write("姓    名                电话                数学        物理        化学        英语");
                temp3.close();
                textEdit->setText("新班级已创建！");
                delete lineEdit;
                delete okButton;
                closeOrOpen(*addNewClass, *modifyClass, *addNewStudent, *modifyStudent, *deleteStudent);
            }
        });
        connect(returnBack, &QPushButton::clicked, [=](){
            closeOrOpen(*addNewClass, *modifyClass, *addNewStudent, *modifyStudent, *deleteStudent);
            textEdit->setText("请选择所需功能！");
            delete lineEdit;
            delete okButton;
            delete returnBack;
        });
    });
    connect(modifyClass,&QPushButton::clicked,this,[=](){
        closeOrOpen(*addNewClass, *modifyClass, *addNewStudent, *modifyStudent, *deleteStudent);
        QLineEdit* lineEdit = new QLineEdit("请输入班级名字", this);
        lineEdit->setFont(font);
        lineEdit->setGeometry(100, 400, 400, 70);
        lineEdit->setStyleSheet(R"(QLineEdit {
                                     background-color: transparent; /* 设置背景为透明 */
                                     border: 2px solid #000000;
                                     border-radius: 5px;
                                     transition: background-color 0.3s ease;
                                     }
                                     QLineEdit:hover {
                                     background-color: #ADD8E6; /* 鼠标悬停时设置背景为浅蓝色 */
                                     })");
        lineEdit->show();
        QPushButton* okButton = new QPushButton("OK", this);
        okButton->setGeometry(120, 550, 120, 70);
        okButton->setFont(font);
        okButton->setStyleSheet("QPushButton { "
                                "background-color: transparent; " //设置背景色为透明
                                "border: 2px solid #000000; " //设置边框为2像素宽，黑色
                                "border-radius: 5px; " // 设置边框圆角半径
                                "transition: background-color 0.3s ease; " //添加过渡效果
                                "}"
                                "QPushButton:hover { "
                                "background-color: #ADD8E6; " //鼠标悬停时设置背景色为浅蓝色
                                "}");
        okButton->show();
        QPushButton * returnBack = new QPushButton("返回",this);
        returnBack->setGeometry(360, 550, 120, 70);
        returnBack->setFont(font);
        returnBack->setStyleSheet("QPushButton { "
                                  "background-color: transparent; " //设置背景色为透明
                                  "border: 2px solid #000000; " //设置边框为2像素宽，黑色
                                  "border-radius: 5px; " // 设置边框圆角半径
                                  "transition: background-color 0.3s ease; " //添加过渡效果
                                  "}"
                                  "QPushButton:hover { "
                                  "background-color: #ADD8E6; " //鼠标悬停时设置背景色为浅蓝色
                                  "}");
        returnBack->show();
        textEdit->setText("请输入班级名，格式为：________班，完成后点击“OK”");
        connect(okButton, &QPushButton::clicked, [=](){
            QFile temp("AllClassName.txt");
            temp.open(QIODevice::ReadOnly | QIODevice::Text);
            QTextStream in(&temp);
            bool ifhave = false;
            while (!in.atEnd()) {
                QString line = in.readLine();
                if (line.contains(lineEdit->text())) {
                    textEdit->setText("已修改所操作班级！");
                    temp.close();
                    operateClass->className = lineEdit->text();
                    delete lineEdit;
                    delete okButton;
                    closeOrOpen(*addNewClass, *modifyClass, *addNewStudent, *modifyStudent, *deleteStudent);
                    ifhave = true;
                    break;
                }
            }
            if(ifhave==false) {
                temp.close();
                textEdit->setText("该班级不存在！");
                delete lineEdit;
                delete okButton;
                closeOrOpen(*addNewClass, *modifyClass, *addNewStudent, *modifyStudent, *deleteStudent);
            }
        });
        connect(returnBack, &QPushButton::clicked, [=](){
            closeOrOpen(*addNewClass, *modifyClass, *addNewStudent, *modifyStudent, *deleteStudent);
            textEdit->setText("请选择所需功能！");
            delete lineEdit;
            delete okButton;
            delete returnBack;
        });
    });
    connect(addNewStudent,&QPushButton::clicked,this,[=](){
        closeOrOpen(*addNewClass, *modifyClass, *addNewStudent, *modifyStudent, *deleteStudent);
        QLineEdit* lineEdit1 = new QLineEdit("请输入新生姓名", this);
        lineEdit1->setFont(font);
        lineEdit1->setGeometry(100, 350, 400, 70);
        lineEdit1->setStyleSheet(R"(QLineEdit {
                                     background-color: transparent; /* 设置背景为透明 */
                                     border: 2px solid #000000;
                                     border-radius: 5px;
                                     transition: background-color 0.3s ease;
                                     }
                                     QLineEdit:hover {
                                     background-color: #ADD8E6; /* 鼠标悬停时设置背景为浅蓝色 */
                                     })");
        lineEdit1->show();
        QLineEdit* lineEdit2 = new QLineEdit("请输入学生/家长电话", this);
        lineEdit2->setFont(font);
        lineEdit2->setGeometry(100, 500, 400, 70);
        lineEdit2->setStyleSheet(R"(QLineEdit {
                                     background-color: transparent; /*  设置背景为透明 */
                                     border: 2px solid #000000;
                                     border-radius: 5px;
                                     transition: background-color 0.3s ease;
                                     }
                                     QLineEdit:hover {
                                     background-color: #ADD8E6; /* 鼠标悬停时设置背景为浅蓝色 */
                                     })");
        lineEdit2->show();
        QPushButton* okButton1 = new QPushButton("OK", this);
        okButton1->setGeometry(120, 650, 120, 70);
        okButton1->setFont(font);
        okButton1->setStyleSheet("QPushButton { "
                                "background-color: transparent; " //设置背景色为透明
                                "border: 2px solid #000000; " //设置边框为2像素宽，黑色
                                "border-radius: 5px; " // 设置边框圆角半径
                                "transition: background-color 0.3s ease; " //添加过渡效果
                                "}"
                                "QPushButton:hover { "
                                "background-color: #ADD8E6; " //鼠标悬停时设置背景色为浅蓝色
                                "}");
        okButton1->show();
        QPushButton* okButton2 = new QPushButton("OK", this);
        okButton2->setGeometry(120, 650, 120, 70);
        okButton2->setFont(font);
        okButton2->setStyleSheet("QPushButton { "
                                 "background-color: transparent; " //设置背景色为透明
                                 "border: 2px solid #000000; " //设置边框为2像素宽，黑色
                                 "border-radius: 5px; " // 设置边框圆角半径
                                 "transition: background-color 0.3s ease; " //添加过渡效果
                                 "}"
                                 "QPushButton:hover { "
                                 "background-color: #ADD8E6; " //鼠标悬停时设置背景色为浅蓝色
                                 "}");
        okButton2->setVisible(false);
        QPushButton * returnBack1 = new QPushButton("返回",this);
        returnBack1->setGeometry(360, 650, 120, 70);
        returnBack1->setFont(font);
        returnBack1->setStyleSheet("QPushButton { "
                                  "background-color: transparent; " //设置背景色为透明
                                  "border: 2px solid #000000; " //设置边框为2像素宽，黑色
                                  "border-radius: 5px; " // 设置边框圆角半径
                                  "transition: background-color 0.3s ease; " //添加过渡效果
                                  "}"
                                  "QPushButton:hover { "
                                  "background-color: #ADD8E6; " //鼠标悬停时设置背景色为浅蓝色
                                  "}");
        returnBack1->show();
        QPushButton * returnBack2 = new QPushButton("返回",this);
        returnBack2->setGeometry(360, 550, 120, 70);
        returnBack2->setFont(font);
        returnBack2->setStyleSheet("QPushButton { "
                                  "background-color: transparent; " //设置背景色为透明
                                  "border: 2px solid #000000; " //设置边框为2像素宽，黑色
                                  "border-radius: 5px; " // 设置边框圆角半径
                                  "transition: background-color 0.3s ease; " //添加过渡效果
                                  "}"
                                  "QPushButton:hover { "
                                  "background-color: #ADD8E6; " //鼠标悬停时设置背景色为浅蓝色
                                  "}");
        returnBack2->setVisible(false);
        textEdit->setText("请输入新生姓名、电话，同名请备注，完成后点击“OK”");
        connect(okButton1, &QPushButton::clicked, [=](){
            lineEdit1->setVisible(false);
            lineEdit2->setVisible(false);
            delete okButton1;
            delete returnBack1;
            okButton2->setVisible(true);
            returnBack2->setVisible(true);
            QCheckBox *checkBox1 = new QCheckBox("数", this);
            connect(checkBox1, &QCheckBox::toggled, [=](){

            });
        });
        connect(returnBack1, &QPushButton::clicked, [=](){
            closeOrOpen(*addNewClass, *modifyClass, *addNewStudent, *modifyStudent, *deleteStudent);
            textEdit->setText("请选择所需功能！");
            delete lineEdit1;
            delete lineEdit2;
            delete okButton1;
            delete okButton2;
            delete returnBack2;
            delete returnBack1;
        });
    });
}
